﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text; 
using MySql.Data.MySqlClient;

namespace MySQLLib
{
    public class Friends_groups
    {
        string[,] _friends_groups;
        private MySqlConnection conn;

        public void setConnection(MySqlConnection connection)
        {
            this.conn = connection;
        }

        public void refresh()
        {
            string[] column = new string[1];
            column[0] = "*";

            this._friends_groups = Query.SelectQuery(this.conn, "friends_groups", column);
        }

        public string[,] Friends_Groups
        {
            get { return this._friends_groups; }
        }

        public void addGroup(int playerid, string name)
        {
            string[] columns = { "playerid", "groupname" };
            string[] values = { playerid.ToString(), name };

            Query.InsertQuery(this.conn, "friends_groups", columns, values);
        }

        public void deleteGroupById(int id)
        {
            Query.DeleteQuery(this.conn, "friends_groups", "id=" + id);
        }

        public void deleteGroupByPlayerId(int playerid)
        {
            Query.DeleteQuery(this.conn, "friends_groups", "playerid=" + playerid);
        }

        public void deleteGroupByName(string name)
        {
            Query.DeleteQuery(this.conn, "friends_groups", "groupname=" + name);
        }

        public void renameGroup(int id, string newname)
        {
            Query.UpdateQuery(this.conn, "friends_groups", "groupname", newname, "id=" + id);
        }

        public string[,] getGroupsFromPlayer(int playerid, bool refresh = false)
        {
            if (refresh)
                this.refresh();

            string[,] groups = new string[0, this._friends_groups.GetLength(1)];

            for (int i = 0; i < this._friends_groups.GetLength(0); i++)
            {
                if (this._friends_groups[i, 1] == playerid.ToString())
                {
                    Other.ResizetwoDimensArray(ref groups, this._friends_groups.GetLength(1), this._friends_groups.GetLength(0) + 1);
                    for (int i2 = 0; i2 < this._friends_groups.GetLength(1); i2++)
                    {
                        groups[groups.GetLength(0) - 1, i2] = this._friends_groups[i, i2];
                    }
                }
            }

            return groups;
        }
    }
}
